
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>6.前后端数据交互说明 · 邦德前端开发</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        <meta name="author" content="i@ickeep.com">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-diy-introduction/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../3.常见问题/常见问题.html" />
    
    
    <link rel="prev" href="5.vue规范.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://2017hcb.bond520.com/pc/index.html" target="_blank" class="custom-link">邦德官网</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        <li class="header">开发说明</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">前端规范</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="1.HTML.html">
            
                <a href="1.HTML.html">
            
                    
                    1.HTML
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="2.CSS.html">
            
                <a href="2.CSS.html">
            
                    
                    2.CSS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="3.JavaScript.html">
            
                <a href="3.JavaScript.html">
            
                    
                    3.JavaScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="4.ESlint检测规则说明.html">
            
                <a href="4.ESlint检测规则说明.html">
            
                    
                    4.ESlint 检测规则说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="5.vue规范.html">
            
                <a href="5.vue规范.html">
            
                    
                    5.vue规范
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.6" data-path="6.前后端数据交互说明.html">
            
                <a href="6.前后端数据交互说明.html">
            
                    
                    6.前后端数据交互说明
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">常见问题</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../3.常见问题/常见问题.html">
            
                <a href="../3.常见问题/常见问题.html">
            
                    
                    常见问题
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >6.前后端数据交互说明</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="&#x524D;&#x7AEF;&#x6570;&#x636E;&#x4EA4;&#x4E92;&#x89C4;&#x8303;"><a name="&#x524D;&#x7AEF;&#x6570;&#x636E;&#x4EA4;&#x4E92;&#x89C4;&#x8303;" class="plugin-anchor" href="#&#x524D;&#x7AEF;&#x6570;&#x636E;&#x4EA4;&#x4E92;&#x89C4;&#x8303;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x524D;&#x7AEF;&#x6570;&#x636E;&#x4EA4;&#x4E92;&#x89C4;&#x8303;</h3>
<blockquote>
<p>JSON&#x6570;&#x636E;&#x8F93;&#x51FA; &#x683C;&#x5F0F;&#xFF1A;</p>
</blockquote>
<pre class="language-"><code>{errno: 0, errmsg: &quot;&quot;, data: {&quot;name&quot;: &quot;thinkjs&quot;}}
</code></pre><blockquote>
<p>&#x5206;&#x9875;&#x683C;&#x5F0F;</p>
</blockquote>
<pre class="language-"><code>{
    errno: 0,
    errmsg: &quot;&quot;,
    data: {
        numsPerPage: 10, //&#x6BCF;&#x9875;&#x663E;&#x793A;&#x7684;&#x6761;&#x6570;
        currentPage: 1, //&#x5F53;&#x524D;&#x9875;
        count: 100, //&#x603B;&#x6761;&#x6570;
        totalPages: 10, //&#x603B;&#x9875;&#x6570;
        data: [{ //&#x5F53;&#x524D;&#x9875;&#x4E0B;&#x7684;&#x6570;&#x636E;&#x5217;&#x8868;
            name: &quot;thinkjs&quot;,
            email: &quot;admin@thinkjs.org&quot;
        }]
    }
}
</code></pre><h3 id="vue-&#x5F02;&#x6B65;&#x8BF7;&#x6C42;&#x5C01;&#x88C5;"><a name="vue-&#x5F02;&#x6B65;&#x8BF7;&#x6C42;&#x5C01;&#x88C5;" class="plugin-anchor" href="#vue-&#x5F02;&#x6B65;&#x8BF7;&#x6C42;&#x5C01;&#x88C5;"><i class="fa fa-link" aria-hidden="true"></i></a>Vue &#x5F02;&#x6B65;&#x8BF7;&#x6C42;&#x5C01;&#x88C5;</h3>
<blockquote>
<ol>
<li>src/http.js &#x4E3A;&#x5C01;&#x88C5;http&#x8BF7;&#x6C42;&#x7684;vue&#x63D2;&#x4EF6;&#xFF0C;&#x8BE6;&#x7EC6;&#x5B9E;&#x73B0;&#x53EF;&#x67E5;&#x770B;&#x6E90;&#x7801;</li>
<li>get &#x8BF7;&#x6C42;&#xFF0C;&#x76F4;&#x63A5;&#x5728;&#x9875;&#x9762;&#xFF08;&#x7EC4;&#x4EF6;&#xFF09;&#x91CC; this.$httpGet(url, opt = {}, toastData = false, callback)<blockquote>
<p>&#x53C2;&#x6570;&#x76F4;&#x63A5;&#x4F20;&#x5165; callback &#x4E0D;&#x63A8;&#x8350;&#xFF0C;&#x672C;&#x8EAB;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; Promise &#x652F;&#x6301; this.httpGet().then() or await this.httpGet</p>
</blockquote>
</li>
<li>post &#x8BF7;&#x6C42;,&#x76F4;&#x63A5;&#x5728;&#x9875;&#x9762;&#xFF08;&#x7EC4;&#x4EF6;&#xFF09;&#x91CC; this.httpPost(url, opt = {}, toastData = &apos;&#x66F4;&#x65B0;&#x6210;&#x529F;&apos;, callback)<blockquote>
<p>&#x4E0E;get&#x8BF7;&#x6C42;&#x7C7B;&#x4F3C;</p>
</blockquote>
</li>
<li>URL &#x5DF2;&#x505A;&#x5904;&#x7406;&#xFF0C;&#x76F4;&#x63A5; this.$httpGet(/api/xxx),&#x8BF7;&#x6C42;api&#x670D;&#x52A1;&#x5668;&#xFF0C;this.httpGet(/test/xxx) &#x8BBF;&#x95EE;&#x672C;&#x5730;&#x81EA;&#x5DF1;&#x751F;&#x6210;&#x7684;json&#x6570;&#x636E;&#x670D;&#x52A1;&#x5668;<blockquote>
<p>DEMO</p>
</blockquote>
</li>
</ol>
</blockquote>
<pre class="language-"><code> methods: {
      fetchData() {
        this.$httpGet(&apos;/api/user&apos;).then((res) =&gt; {
          // &#x8BF7;&#x6C42;&#x6210;&#x529F;
          if (res.errcode === 0) {
            // &#x6210;&#x529F;
          } else {
            // &#x5931;&#x8D25;
          }
        }, (res) =&gt; {
          // &#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x5931;&#x8D25; &#xFF08;&#x901A;&#x5E38;&#x4E0D;&#x9700;&#x8981;&#x505A;&#x5904;&#x7406;&#xFF0C;&#x5DF2;&#x5728;http&#x91CC;&#x5904;&#x7406;&#x5B8C;&#x6210;
        })
      }
    }
</code></pre><h3 id="vue-&#x957F;&#x8FDE;&#x63A5;&#x5C01;&#x88C5;"><a name="vue-&#x957F;&#x8FDE;&#x63A5;&#x5C01;&#x88C5;" class="plugin-anchor" href="#vue-&#x957F;&#x8FDE;&#x63A5;&#x5C01;&#x88C5;"><i class="fa fa-link" aria-hidden="true"></i></a>Vue &#x957F;&#x8FDE;&#x63A5;&#x5C01;&#x88C5;</h3>
<blockquote>
<ol>
<li>src/socket.js &#x4E3A;&#x5C01;&#x88C5;socket.io&#x7684;vue&#x63D2;&#x4EF6;&#xFF0C;&#x8BE6;&#x7EC6;&#x5B9E;&#x73B0;&#x53EF;&#x67E5;&#x770B;&#x6E90;&#x7801;(&#x975E;&#x5E38;&#x7B80;&#x5355;)</li>
<li>&#x4E8B;&#x4EF6;&#x76D1;&#x542C; &#x4F7F;&#x7528;  this.$socket.on(event,callback)</li>
</ol>
</blockquote>
<pre class="language-"><code>  this.$socket.on(&apos;pushtopic&apos;, (res) =&gt; {
      console.log(res);
  })
</code></pre><blockquote>
<ol>
<li>&#x53D1;&#x9001;&#x4E8B;&#x4EF6; &#x4F7F;&#x7528; this.$socket.emit(event,data)</li>
</ol>
</blockquote>
<pre class="language-"><code>this.$socket.emit(&apos;topic&apos;, { id: 1, name: &apos;&#x6570;&#x5B66;&apos; })
</code></pre><footer class="page-footer"><span class="copyright">Copyright &#xA9; doc.ickeep.com 2017 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2017-02-21 20:52:38
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="5.vue规范.html" class="navigation navigation-prev " aria-label="Previous page: 5.vue规范">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../3.常见问题/常见问题.html" class="navigation navigation-next " aria-label="Next page: 常见问题">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"6.前后端数据交互说明","level":"2.6","depth":1,"next":{"title":"常见问题","level":"3.1","depth":1,"path":"3.常见问题/常见问题.md","ref":"3.常见问题/常见问题.md","articles":[]},"previous":{"title":"5.vue规范","level":"2.5","depth":1,"path":"2.前端规范/5.vue规范.md","ref":"2.前端规范/5.vue规范.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","prism","-highlight","search-plus","tbfed-pagefooter","expandable-chapters-small","anchors","edit-link","diy-introduction","sitemap-general","livereload"],"root":"./docs","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy doc.ickeep.com 2017","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"introduction-text":"开发说明","prism":{},"livereload":{},"sitemap-general":{"prefix":"https://doc.ickeep.com"},"fontsettings":{"theme":"white","family":"sans","size":2},"fontSettings":{"theme":"night","family":"sans","size":2},"diy-introduction":{},"favicon":{"shortcut":"/assets/img/favicon.ico","bookmark":"/assets/img/favicon.ico","appleTouch":"/assets/img/apple-touch-icon.png","appleTouchMore":{"120x120":"/assets/img/apple-touch-icon-120x120.png","180x180":"/assets/img/apple-touch-icon-180x180.png"}},"expandable-chapters-small":{},"sharing":{"facebook":false,"twitter":false,"google":false,"weibo":true,"instapaper":false,"vk":false,"all":["facebook","google","twitter","instapaper","instapaper"]},"edit-link":{"label":"编辑此页","base":"https://git.oschina.net/ickeep/dev-doc.git/edit/master"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{},"search-plus":{}},"theme":"default","author":"i@ickeep.com","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.MD","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"邦德前端开发","language":"zh-hans","links":{"sidebar":{"邦德官网":"http://2017hcb.bond520.com/pc/index.html"}},"gitbook":"*","description":"邦德前端开发文档"},"file":{"path":"2.前端规范/6.前后端数据交互说明.md","mtime":"2017-02-21T12:52:38.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-03-10T13:25:56.104Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

